package hsmoto.supportClass
{
	import flash.display.DisplayObject;
	import flash.geom.Rectangle;
	
	import hsmoto.core.Tool;
	import hsmoto.core.UIComponent;
	import hsmoto.data.PropertyInfo;

	/**
	 * 基本布局是Group的默认布局，使用此布局其子UIComponent的所有位置属性都有效
	 * @author Zhang
	 * 
	 */	
	public class BasicLayout extends LayoutBase
	{
		public function BasicLayout()
		{
			
		}
		
		override public function needUpdate():Boolean
		{
			var rect1:Rectangle = measure();
			var rect2:Rectangle = targetRect;
			var changed:Boolean = rect1.width != rect2.width || rect1.height != rect2.height || rect1.x != rect2.x || rect1.y != rect2.y;
			targetRect = rect1;
			return changed;
		}
		
		override public function updateDisplayList():void
		{
			if(!this.target)
				return;
			var child:UIComponent, lastH:PropertyInfo, lastV:PropertyInfo, rect:Rectangle;
			this.targetRect = measure();
			for each(child in this.target.uiChildren)
			{
				rect = Tool.getScaleBounds(child);
				lastH = child.lastHorizontalSet;
				lastV = child.lastVerticalSet;
				Tool[lastH.name](child, lastH.value, rect, this.targetRect);
				Tool[lastV.name](child, lastV.value, rect, this.targetRect);
			}
		}
		
		override protected function measure():Rectangle
		{
			if(this.target.scrollRect)
				return Tool.getScaleBounds(this.target);
			var child:DisplayObject, rect:Rectangle, num:int = this.target.numChildren, lastH:PropertyInfo, lastV:PropertyInfo;
			for (var i:int = 0; i < num; i++)
			{
				child = this.target.getChildAt( i );
				rect = Tool.getScaleBounds(child);
				if(child is UIComponent)
				{
					lastH = (child as UIComponent).lastHorizontalSet;
					lastV = (child as UIComponent).lastVerticalSet;
					if(lastH.name == "x")
					{
						Tool.addToRect(child.x, 0, rect.width, rect.height);
					}
					else if(lastH.name == "left")
					{
						Tool.addToRect(lastH.value - rect.x, 0, rect.width, rect.height);
					}
					if(lastV.name == "y")
					{
						Tool.addToRect(0, child.y, rect.width, rect.height);
					}
					else if(lastV.name == "top")
					{
						Tool.addToRect(0, lastV.value - rect.y, rect.width, rect.height);
					}
				}
				else
				{
					Tool.addToRect(child.x, child.y, rect.width, rect.height);
				}
			}
			return Tool.getRect(this.target);
		}
	}
}